package groep3.ssc.coachViews;

import groep3.ssc.controllers.ModelRepository;
import groep3.ssc.controllers.ViewController;
import groep3.ssc.main.Main;
import groep3.ssc.main.R;
import groep3.ssc.main.SkateAppPagerAdapter.Pages;
import groep3.ssc.model.Race;
import groep3.ssc.model.Skater;
import groep3.ssc.model.Skater.Lane;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NewRaceManager extends Activity
{
    private TextView txtFldId, txtFldLength;
    private Spinner sprLane;
    private Button btnAccept;
    private String strLane;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scheme_new_race);
        initFields();
    }

    private void initFields()
    {
        txtFldId = (TextView) this.findViewById(R.id.txtFldId);
        txtFldLength = (TextView) this.findViewById(R.id.txtFldLength);

        sprLane = (Spinner) this.findViewById(R.id.sprLane);
        ArrayAdapter<CharSequence> laneAdapter = ArrayAdapter.createFromResource(this, R.array.lane,
                android.R.layout.simple_list_item_1);
        sprLane.setAdapter(laneAdapter);
        sprLane.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                strLane = sprLane.getSelectedItem().toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
            }
        });

        btnAccept = (Button) this.findViewById(R.id.btnAccept);
        btnAccept.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                if (checkFields())
                {
                    acceptChanges();
                }
            }
        });
    }

    private void acceptChanges()
    {
        ModelRepository repository = ModelRepository.getInstance();
        Race race = repository.getNewRace();

        int skaterId = Integer.valueOf(txtFldId.getText().toString());

        Skater skater = new Skater();
        if (strLane.equals(getString(R.string.txtInner)))
        {
            skater.setLane(Lane.INNER);
        } else if (strLane.equals(getString(R.string.txtOuter)))
        {
            skater.setLane(Lane.OUTER);
        }

        race.addSkater(skaterId, skater);
        race.setOurSkater(skaterId);
        race.setDistanceToSkate(Integer.valueOf(txtFldLength.getText().toString()));

        repository.setCurrentRaceId(race.getId());

        Toast.makeText(getApplicationContext(), R.string.txtNewRaceStarted, Toast.LENGTH_SHORT).show();
        ViewController.getInstance().update();
        Main.viewPager.setCurrentItem(Pages.SKATERVIEW.ordinal());
    }

    private boolean checkFields()
    {
        if (txtFldId.getText().toString().equals(""))
        {
            messageDialog(getString(R.string.txtNoSkaterID));
            return false;
        } else if (txtFldLength.getText().toString().equals(""))
        {
            messageDialog(getString(R.string.txtNoDistance));
            return false;
        }
        return true;
    }

    private void messageDialog(String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.txtTitelDialogNewRace));
        builder.setMessage(message);
        builder.setCancelable(false);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

}
